import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
image = cv.imread(r"D:\AI\image\chessboard.png")
image2 = cv.imread(r'D:\AI\image\aero3.jpg')
image3 = cv.imread(r"D:\AI\image\left03.jpg")
image4 = cv.imread(r'D:\AI\image\left04.jpg')

#SIFT算子
def SIFT(img,img2):
    img = cv.resize(img, (400, 400), interpolation=cv.INTER_CUBIC)
    img2 = cv.resize(img2, (400, 400), interpolation=cv.INTER_CUBIC)

    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    gray2 = cv.cvtColor(img2, cv.COLOR_BGR2GRAY)

    sift = cv.xfeatures2d.SIFT_create()
    sift2 = cv.xfeatures2d.SIFT_create()

    keypoints, descriptor = sift.detectAndCompute(gray, None)
    keypoints2, descriptor2 = sift2.detectAndCompute(gray2, None)

    chessboard_SIFT = cv.drawKeypoints(img, keypoints, img, color=(255, 255, 255))
    aero3_SIFT = cv.drawKeypoints(img2, keypoints2, img2, color=(255, 255, 255))

    cv.imshow('chessboard_sift', chessboard_SIFT)
    cv.imshow('aero3_sift', aero3_SIFT)

#ORB
def ORB(img,img2):
    # 图片大小重置
    img = cv.resize(img, (400, 400), interpolation=cv.INTER_CUBIC)
    img_1 = cv.resize(img2, (400, 400), interpolation=cv.INTER_CUBIC)
    # 创建orb特征检测器和描述符
    orb = cv.ORB_create()
    kp, des = orb.detectAndCompute(img, None)
    kp_1, des_1 = orb.detectAndCompute(img, None)
    # 画出关键点
    img2 = cv.drawKeypoints(img, kp, None, color=(255, 255, 255))
    img2_1 = cv.drawKeypoints(img_1, kp_1, None, color=(255, 255, 255))
    cv.imshow("chessboard_orb", img2)
    cv.imshow("aero3_orb", img2_1)
##Fast
def Fast(img,img2):
    fast=cv.FastFeatureDetector_create(threshold=40,
                                       nonmaxSuppression=True,
                                       type=cv.FAST_FEATURE_DETECTOR_TYPE_9_16)
    img = cv.resize(img, (400, 400), interpolation=cv.INTER_CUBIC)
    img2 = cv.resize(img2, (400, 400), interpolation=cv.INTER_CUBIC)
    kp = fast.detect(img,None)
    kp2 = fast.detect(img2, None)
    cv.drawKeypoints(img,kp,img,color=(255,0,0))
    cv.drawKeypoints(img2, kp2, img2, color=(255, 255, 255))
    cv.imshow("chessboard_fast",img)
    cv.imshow("aero3_fast",img2)
#SURF算子
def SURF(img,img2):
    minHessian=1000
    detector=cv.xfeatures2d.SURF_create((minHessian))
    descriptor=cv.xfeatures2d.SURF_create()
    matcher1=cv.DescriptorMatcher_create("BruteForce")
    #检测特征点
    keyPoint1=detector.detect(img)
    keyPoint2=detector.detect(img2)
    #计算特征点对应描述
    _,descriptor1=descriptor.compute(img,keyPoint1)
    _,descriptor2=descriptor.compute(img2,keyPoint2)
    #描述子匹配
    matches=matcher1.match(descriptor1,descriptor2)
    img_matches=np.empty(img.shape)
    img_matches1=cv.drawMatches(img,keyPoint1,img2,keyPoint2,matches,img_matches)
    cv.imshow("img_matches",img_matches1)


##Harris算子
def Harris(img,img2):
    img = cv.resize(img, (600, 600), interpolation=cv.INTER_CUBIC)
    img2 = cv.resize(img2, (600, 600), interpolation=cv.INTER_CUBIC)

    gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    gray2 = cv.cvtColor(img2, cv.COLOR_BGR2GRAY)
    dst = cv.cornerHarris(gray, 2, 3, 0.04)
    dst_1 = cv.cornerHarris(gray2, 2, 3, 0.04)
    # 0.04是角点响应函数里面的系数，属于经验值一般取0.04-0.06

    dst = cv.dilate(dst,None)#图像膨胀 目的是把角点的标记变大些
    dst_1 = cv.dilate(dst_1,None)

    img[dst > 0.00001 * dst.max()] = [0, 255, 0]
    img2[dst_1 > 0.001 * dst_1.max()] = [0, 255, 0]

    cv.imshow("chess_board Harris", img)
    cv.imshow("aero3 Harris", img2)

#Harris(image,image2)
Fast(image,image2)
#SURF(image3,image4)
#SIFT(image,image2)
#ORB(image,image2)
cv.waitKey()
cv.destroyAllWindows()